export const createStore  = (reducer, enhancer) => {
  if(enhancer){
    return enhancer(createStore)(reducer)
  }
  let listeners = [],
    currentState = {}

  function getState(){
    return currentState
  }
  function subscribe(listener){
    listeners.push(listener)
  }
  function dispatch(action){
    currentState = reducer(currentState, action)
    listeners.forEach(v => v())
  }
  dispatch({
    type: `@LAOFULIAOFASHAONIANKUANG&ZUOQIANHUANGYOUQINCANG`
  })
  return {
    getState,
    dispatch,
    subscribe
  }
}
export const applyMiddleware = (middleware) => (createStore) => (...args) => {
  const store = createStore(...args)
  let dispatch = store.dispatch
  const midApi = {
    getState: store.getState,
    dispatch: (...args) => dispatch(...args)
  }
  dispatch = middleware(midApi)(store.dispatch)
  return {
    ...store,
    dispatch
  } 
}

function bindDispatch(creator, dispatch){
  return (...args) => {
    dispatch(creator(...args))
  }
}
export const bindActionCreator = (creators, dispatch) => {
  const bound = {}
  Object.keys(creators).forEach(v => {
    bound[v] = bindDispatch(creators[v], dispatch)
  })
  return bound
}